import 'package:flutter_tts/flutter_tts.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_flutter.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
//与objectbox数据库一起使用报错
//import 'package:flutter_tts/flutter_tts_web.dart';

/*
文字转语音 ANDROID IOS MACOS WEB WINDOWS
https://pub.flutter-io.cn/packages/flutter_tts
dependencies:
  flutter_tts: ^3.6.3

 */
abstract class XTTSUtils{

  static final FlutterTts _flutterTts=FlutterTts();
  // static TtsState ttsState = TtsState.stopped;
  // static get isPlaying => ttsState == TtsState.playing;
  // static get isStopped => ttsState == TtsState.stopped;
  // static get isPaused => ttsState == TtsState.paused;
  // static get isContinued => ttsState == TtsState.continued;

  //1.初始化tts
  static void initTts(
      {
        double volume = 1.0,
        double pitch = 1.0,
        double rate = 1.0,
      }
      ) async{

    try{

      await _flutterTts.awaitSpeakCompletion(true);
      //await flutterTts.awaitSynthCompletion(true);

      if (XFlutterUtils.isAndroid()) {
        //只支持Android
        var engine = await _flutterTts.getDefaultEngine;
        var voice = await _flutterTts.getDefaultVoice;
        int? maxSpeechInputLength=await _flutterTts.getMaxSpeechInputLength;

        XLogUtils.printLog('---android TTS init,engine=$engine, voice=$voice, maxSpeechInputLength=$maxSpeechInputLength');
      }

      if(XFlutterUtils.isiOS()){
        await _flutterTts.setSharedInstance(true);

        await _flutterTts.setIosAudioCategory(IosTextToSpeechAudioCategory.ambient,
            [
              IosTextToSpeechAudioCategoryOptions.allowBluetooth,
              IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
              IosTextToSpeechAudioCategoryOptions.mixWithOthers
            ],
            IosTextToSpeechAudioMode.voicePrompt
        );
      }



      _flutterTts.setStartHandler(() {
        //print("Playing");
      });


      _flutterTts.setCompletionHandler(() {
        //print("Complete");
      });

      _flutterTts.setCancelHandler(() {
        //print("Cancel");
      });

      _flutterTts.setPauseHandler(() {
        //print("Paused");
      });

      _flutterTts.setContinueHandler(() {
        //print("Continued");
      });

      _flutterTts.setErrorHandler((msg) {
        XLogUtils.printLog('---tts error: $msg');
      });


      await _flutterTts.setVolume(volume);
      await _flutterTts.setSpeechRate(rate);
      await _flutterTts.setPitch(pitch);



    }catch(e){
      XLogUtils.printLog('$e');
    }



  }


  //2.输出语音
  static Future speak(String? voiceText) async {
    if (!XStringUtils.isEmpty(voiceText)) {
      await _flutterTts.speak(voiceText!);
    }
  }

  //速度:0~1
  static Future setSpeechRate(double rate) async {
    await _flutterTts.setSpeechRate(rate);
  }

  //停止输出语音
  static Future stop() async {
    await _flutterTts.stop();
  }

  //暂停输出语音
  static Future pause() async {
    await _flutterTts.pause();
  }


  /*
  Future _speak() async{
    var result = await flutterTts.speak("Hello World");
    if (result == 1) setState(() => ttsState = TtsState.playing);
}

Future _stop() async{
    var result = await flutterTts.stop();
    if (result == 1) setState(() => ttsState = TtsState.stopped);
}

List<dynamic> languages = await flutterTts.getLanguages;

await flutterTts.setLanguage("en-US");

await flutterTts.setSpeechRate(1.0);

await flutterTts.setVolume(1.0);

await flutterTts.setPitch(1.0);

await flutterTts.isLanguageAvailable("en-US");

// iOS, Android and Web only
//see the "Pausing on Android" section for more info
await flutterTts.pause();

// iOS, macOS, and Android only
await flutterTts.synthesizeToFile("Hello World", Platform.isAndroid ? "tts.wav" : "tts.caf");

await flutterTts.setVoice({"name": "Karen", "locale": "en-AU"});

// iOS only
await flutterTts.setSharedInstance(true);

// Android only
await flutterTts.setSilence(2);

await flutterTts.getEngines;

await flutterTts.getDefaultVoice;

await flutterTts.isLanguageInstalled("en-AU");

await flutterTts.areLanguagesInstalled(["en-AU", "en-US"]);

await flutterTts.setQueueMode(1);

await flutterTts.getMaxSpeechInputLength;
   */

  Future<dynamic> _getLanguages() async => await _flutterTts.getLanguages;

  Future<dynamic> _getEngines() async => await _flutterTts.getEngines;


}
